﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace ArmoryViewer
{
	/// <summary>
	/// Interaction logic for ErrorWindow.xaml
	/// </summary>
	public partial class ErrorWindow : Window
	{
        Exception exception = new Exception("No error...");
        TaskbarManager Taskbar;
        TaskDialog tdError;

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorWindow"/> class.
        /// </summary>
		public ErrorWindow()
		{
			this.InitializeComponent();
			if (TaskbarManager.IsPlatformSupported) Taskbar = TaskbarManager.Instance;
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorWindow"/> class.
        /// </summary>
        /// <param name="ex">The exception.</param>
        public ErrorWindow(Exception ex) : this()
        {
            exception = ex;
            if (App.IsWindowsVista)
                try { ShowTaskDialog(); }
                catch { Show(); }
            else Show();
        }

        private void ShowTaskDialog()
        {
            if (TaskbarManager.IsPlatformSupported)
            {
                Taskbar.SetProgressState(TaskbarProgressBarState.Error);
                Taskbar.SetProgressValue(100, 100);
            }

            tdError = new TaskDialog();
            tdError.DetailsExpanded = false;
            tdError.Cancelable = true;
            tdError.Icon = TaskDialogStandardIcon.Error;

            tdError.Caption = "Error Report";
            tdError.InstructionText = "An unexpected error occured. It is highly recommended that you send the error report.";
            tdError.Text = exception.Message;
            tdError.DetailsExpandedLabel = "Hide details";
            tdError.DetailsCollapsedLabel = "Show details";
            tdError.DetailsExpandedText = Details;

            tdError.ExpansionMode = TaskDialogExpandedDetailsLocation.ExpandFooter;

            TaskDialogCommandLink sendButton = new TaskDialogCommandLink("sendButton", "Send Feedback");
            sendButton.Click += new EventHandler(sendButton_Click);
            TaskDialogCommandLink dontSendButton = new TaskDialogCommandLink("dontSendButton", "No Thanks");
            dontSendButton.Click += new EventHandler(dontSendButton_Click);

            tdError.Controls.Add(sendButton);
            tdError.Controls.Add(dontSendButton);

            tdError.Show();

            if (TaskbarManager.IsPlatformSupported)
            {
                Taskbar.SetProgressValue(0, 0);
                Taskbar.SetProgressState(TaskbarProgressBarState.NoProgress);
            }
        }

        void dontSendButton_Click(object sender, EventArgs e)
        {
            tdError.Close();
        }

       
        private string Details
        {
            get
            {
                string details = "Operating System: " + Environment.OSVersion;
                try
                {
                    details += Environment.NewLine + "--------------" + Environment.NewLine +
                    exception.Message;
                }
                catch { }

                try
                {
                    details += Environment.NewLine + exception.StackTrace;
                }
                catch { }

                try
                {
                    details += Environment.NewLine + "--------------" + Environment.NewLine +
                    exception.InnerException.Message;
                }
                catch { }

                try
                {
                    details += Environment.NewLine + exception.InnerException.StackTrace;
                }
                catch { }
                return details;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtContents.Text = Details;
            txtContents.Select(0, 0);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Send();
            Close();
        }

        void sendButton_Click(object sender, EventArgs e)
        {
            Send();
            tdError.Close();
        }

        void Send()
        {
            Process.Start("http://armoryviewer.codeplex.com/WorkItem/Create.aspx");
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
	}
}